<?php
class Cal_model extends CI_Model{
	

	
	function __construct(){
		parent::__construct();
		
	}
	
	
	function get_calendar($id){
		$c = $this->db->get_where('calendars', array('user_id'=>$id));
		if($c->num_rows() == 1){
			$cal = $c->row();
			return $cal->calendar_id;
		}
		else {return FALSE;}
		
	}
	
	function create_cal($id){
		$this->db->insert('calendars', $id);
		
	}
	
	function get_events($cal_id){
		$e = $this->db->get_where('events', array('calendar_id'=>$cal_id));
		if($e->num_rows() > 0){
			return $e;
		}
		else {return FALSE;}
	}
	
	function edit_event($date, $time, $content){
		$d = date('Y-m-d', strtotime($date));
		$t = date('H:i:s', strtotime($time));
		$datetime = $d . " " . $t;
		$mod = date('Y-m-d H:i:s', strtotime("now"));
		
		$obj = array(
			'datetime_start' => $datetime,
			'content' => $content,
			'datetime_modified' => $mod
		);
		
		$this->db->update('calendar', $obj, 'datetime_start = "2011-04-28 19:00:00"');
	
	}
	
	function add_event($data){
		$this->db->insert('events',$data);
	}
	
	function get_today_event($today, $c_id){
		$e = $this->db->get_where('events', array('calendar_id'=>$c_id));
		if($e->num_rows() > 0){
			$events = array();
			foreach($e->result() as $ev){
				$ev_time = substr($ev->start,0,10);
				if($today == $ev_time){
					$events[] = $ev;
				}
			}
			return $events;
		}
		else return FALSE;
	}
}